package main

import (
    "fmt"
    "net/http"
    "encoding/json"
    "io/ioutil"
    "strings"
)

type ResponseData struct {  
    Code    int         `json:"code"`  
    Data    interface{} `json:"data"`  
    Message string      `json:"message"`  
}  
  
func NewResponseData() ResponseData {  
    return ResponseData{}  
} 

func sendResponse(w http.ResponseWriter, status int, result ResponseData) {

    w.Header().Set("Connection", "close")
    w.WriteHeader(status)
    bytes, _ := json.Marshal(result)  
    fmt.Fprint(w, string(bytes))
}

func hander(w http.ResponseWriter, r *http.Request) {

    result := NewResponseData()//创建一个返回结构体  

    if r.URL.Path != "/_acme" || r.Method != "POST" {
        result.Code = 0  
        result.Message = "错误的路径或请求方法"  
        sendResponse(w, 404, result)
        return
    }

    if (len(g_config.Acl) > 0) {
        addr := r.RemoteAddr
        pos := strings.Index(addr, ":")
        if pos != -1 {
            addr = addr[0:pos]
        }
        found := false
        for _ , v := range g_config.Acl {
            if len(v) > 0 && v == addr {
                found = true
            }
        }
        if !found {
            result.Code = 0  
            result.Message = "IP访问受限"  
            mainlog.Println("IP访问受限\n")  
            sendResponse(w, 403, result)
            return
        }
    }

    mapData := make(map[string]interface{})
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        result.Code = 0  
        result.Message = err.Error() 
        sendResponse(w, 500, result)
        return
    }
    err = json.Unmarshal(body, &mapData)
    if err != nil {
        result.Code = 0  
        result.Message = err.Error() 
        sendResponse(w, 400, result)
        return
    }

    if _, ok := mapData["domain"]; ok {  
        domain = mapData["domain"].(string)//通过.(string)转换成string类型
        mainlog.Println("获取domain成功")
        mainlog.Println("域名:", domain)
    }else {
        //向客户端返回JSON数据  
        result.Code = 0  
        result.Message = "请上传域名"  
        mainlog.Println("未解析到域名\n")  
        sendResponse(w, 400, result)
        return  
    }

    if _, ok := mapData["uid"]; ok {  
        uid = mapData["uid"].(float64)
        mainlog.Println("获取uid成功")
        mainlog.Println("uid:", uid)
    }else {
        //向客户端返回JSON数据  
        result.Code = 0  
        result.Message = "获取uid失败"  
        mainlog.Println("获取uid失败\n")  
        sendResponse(w, 400, result)
        return  
    }

    if _, ok := mapData["id"]; ok {  
        id = mapData["id"].(float64)
        mainlog.Println("获取id成功")
        mainlog.Println("id:", id)
    }else {
        mainlog.Println("未传id值")
        id = -1
    }

    if _, ok := mapData["api"]; ok {  
        api = mapData["api"].(string)
        mainlog.Println("获取api成功")
        mainlog.Println("api:", api)
    }else {
        //向客户端返回JSON数据  
        result.Code = 0  
        result.Message = "获取中心服务器url失败"  
        mainlog.Println("获取中心服务器url失败\n")  
        sendResponse(w, 400, result)
        return  
    }

    if _, ok := mapData["token"]; ok {  
        token = mapData["token"].(string)
        mainlog.Println("获取token成功")
        mainlog.Println("token:", token)
    }else {
        //向客户端返回JSON数据  
        result.Code = 0  
        result.Message = "获取token失败"  
        mainlog.Println("获取token失败\n")  
        sendResponse(w, 400, result)
        return  
    }

    result.Code = 1  
    result.Message = "证书申请成功"
    sendResponse(w, 200, result)

    go acme(w, result)
}